package com.hx.springboot1.service.impl;

import com.hx.springboot1.dao.UserRoleDao;
import com.hx.springboot1.pojo.entity.TabUserRole;
import com.hx.springboot1.service.UserRoleService;
import com.hx.springboot1.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleDao userRoleDao;
    @Override
    public List<TabUserRole> selectRolesById(int id){
        List<TabUserRole> TabUserRoles = userRoleDao.selectByUserId(id);
        return TabUserRoles;
    }

    @Override
    public ResultMsg insertUserRoles(String roleId,Integer userId) {
        int effectRows = 0;
        /* 先删除  再增加 */
        if (roleId==null||userId==null){
            return new ResultMsg("角色或用户ID不能为空！",false);
        }
        String getIds = roleId.substring(9);
        String[] roleIds = getIds.split(",");
        effectRows += userRoleDao.delUserRoles(userId);
        for (String id : roleIds) {
            effectRows += userRoleDao.insertUserRoles(userId, Integer.parseInt(id));
        }
        if (effectRows>0){
            return new ResultMsg("分配角色成功！",true);
        }else {
            return new ResultMsg("分配角色失败！",false);
        }
    }
}
